home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Tertiary Source / MovableModalDialogs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-23  |  6.2 KB  |  220 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3.         FILE:    Originally    AppDialogs.c++
  4.                 Renamed as    MovableModalDialogs.cpp
  5.         
  6.         CREATED:  MAY 29, 1994
  7.         
  8.         BY: Hiep Dam
  9.         Copyright© 1994 by Hiep Dam, All Rights Reserved
  10.         
  11.         INFO: MovableModalDialogs.cpp contains routines that deal with both
  12.               modeless and movable modal dialog boxes. But mainly
  13.               movable modal. Just don't let the name fool you.
  14.  
  15.               This file serves as a "dispatching center"; it collects info
  16.               concerning events sent to it by the main event loop (mousedowns,
  17.               updates, etc.), determines which dialog the event pertains to,
  18.               and calls the appropriate handler.
  19.  
  20.         VERSION HISTORY:
  21.             2.0    Nov 8 95
  22.                 Completely updated code so it can be plugged into any
  23.                 application seamlessly. And doing it required very few
  24.                 changes, which tells me this is well written.
  25.                 I'm so proud...
  26. */
  27.  
  28. #include "MovableModalDialogs.h"
  29. #include "DialogUtils.h"
  30. #include "QDUtils.h"
  31. #include "Class_DynamoArray.cpp"
  32.  
  33. // ---------------------------------------------------------------------------
  34.  
  35. typedef struct DialogHandlers {
  36.     DialogPtr                theDialog;            // Pointer to the dialog
  37.     long                    dialogID;            // Optional id in refcon of dialog
  38.     DialogUpdateHandler     updateHandler;
  39.     DialogHitHandler         hitHandler;
  40.     DialogKeyHandler        keyHandler;
  41.     DialogActivateHandler    actHandler;
  42.     DialogIdleHandler        idleHandler;
  43. } *DialogHandlersPtr, **DialogHandlersHdl;
  44.  
  45. static DynamoArray<DialogHandlersPtr> gDialogHandlers;
  46. static AdjustMenusProc sAdjustMenusProc = NULL;
  47.  
  48. // ---------------------------------------------------------------------------
  49.  
  50. void InitMovableModalDialogs(AdjustMenusProc adjustMenus) {
  51.     sAdjustMenusProc = adjustMenus;
  52. } // END InitMovableModalDialogs
  53.  
  54. // ---------------------------------------------------------------------------
  55.  
  56. void RegisterMovableModalDialog(
  57.     DialogPtr                theDialog,
  58.     DialogHitHandler        hitProc,
  59.     DialogKeyHandler        keyProc,
  60.     DialogUpdateHandler        updateProc,
  61.     DialogActivateHandler    activateProc,
  62.     DialogIdleHandler        idleProc) {
  63.  
  64.     DialogHandlersPtr theHandler;
  65.     theHandler = (DialogHandlersPtr)NewPtr(sizeof(DialogHandlers));
  66.  
  67.     theHandler->theDialog        = theDialog;
  68.     theHandler->dialogID        = GetWRefCon(theDialog);
  69.     theHandler->updateHandler    = updateProc;
  70.     theHandler->hitHandler        = hitProc;
  71.     theHandler->keyHandler        = keyProc;
  72.     theHandler->actHandler        = activateProc;
  73.     theHandler->idleHandler        = idleProc;
  74.  
  75.     gDialogHandlers.Append(theHandler);
  76. } // END RegisterMovableModalDialog
  77.  
  78. // ---------------------------------------------------------------------------
  79.  
  80. void UnregisterMovableModalDialog(DialogPtr theDialog) {
  81.     short i;
  82.     DialogHandlersPtr oneHandler;
  83.  
  84.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  85.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  86.  
  87.         if (oneHandler->theDialog == theDialog) {
  88.             gDialogHandlers.Delete(i);
  89.             DisposePtr((Ptr)oneHandler);
  90.             break;
  91.         }
  92.     }
  93.     
  94.     // By unregistering, we're assuming (pretty safely) that
  95.     // the caller is closing the dialog box. So now we'll
  96.     // call the update menu procedure.
  97.     sAdjustMenusProc();
  98. } // END UnregisterMovableModalDialog
  99.  
  100. // ---------------------------------------------------------------------------
  101.  
  102. void HandleUpdateDialog(DialogPtr theDialog, EventRecord *theEvt, Boolean inBackground) {
  103.     GrafPtr savePort;
  104.     short i;
  105.     Boolean frontMost;
  106.     DialogHandlersPtr oneHandler;
  107.  
  108.     GetPort(&savePort);
  109.  
  110.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  111.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  112.  
  113.         if (oneHandler->theDialog == theDialog) {
  114.             if (inBackground || FrontWindow() != theDialog)
  115.                 frontMost = false;
  116.             else
  117.                 frontMost = true;
  118.  
  119.             SetPort(theDialog);
  120.             if (oneHandler->updateHandler != NULL)
  121.                 oneHandler->updateHandler(theDialog, theEvt, frontMost);
  122.             break;
  123.         }
  124.     }
  125.  
  126.     SetPort(savePort);
  127. } // END HandleUpdateDialog
  128.  
  129. // ---------------------------------------------------------------------------
  130.  
  131. void HandleContentClickModeless(EventRecord *theEvt, DialogPtr theDialog, short itemHit) {
  132.     DialogHandlersPtr oneHandler;
  133.     short i;
  134.  
  135.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  136.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  137.  
  138.         if (oneHandler->theDialog == theDialog) {
  139.             if (oneHandler->hitHandler != NULL)
  140.                 oneHandler->hitHandler(theDialog, theEvt, itemHit);
  141.             return;
  142.         }
  143.     }
  144. } // END HandleContentClickModeless
  145.  
  146. // ---------------------------------------------------------------------------
  147.  
  148. Boolean HandleKeyDownModeless(DialogPtr theDialog, EventRecord *theEvt) {
  149.     short i;
  150.     DialogHandlersPtr oneHandler;
  151.     char theKey = theEvt->message & charCodeMask;
  152.  
  153.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  154.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  155.  
  156.         if (oneHandler->theDialog == theDialog) {
  157.             if (oneHandler->keyHandler != NULL)
  158.                 return(oneHandler->keyHandler(theDialog, theEvt));
  159.             break;
  160.         }
  161.     }
  162.     
  163.     return(false);
  164. } // END HandleKeyDownInModeless
  165.  
  166. // ---------------------------------------------------------------------------
  167.  
  168. void HandleActivateModeless(DialogPtr theDialog, Boolean activate) {
  169.     short i;
  170.     GrafPtr savePort;
  171.     DialogHandlersPtr oneHandler;
  172.  
  173.     GetPort(&savePort);
  174.  
  175.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  176.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  177.  
  178.         if (oneHandler->theDialog == theDialog) {
  179.             SetPort(theDialog);
  180.             if (oneHandler->actHandler != NULL)
  181.                 oneHandler->actHandler(theDialog, activate);
  182.             break;
  183.         }
  184.     }
  185.     SetPort(savePort);
  186. } // END HandleActivateModeless
  187.  
  188. // ---------------------------------------------------------------------------
  189.  
  190. void HandleIdleModeless(DialogPtr theDialog) {
  191.     short i;
  192.     GrafPtr savePort;
  193.     DialogHandlersPtr oneHandler;
  194.  
  195.     GetPort(&savePort);
  196.     for (i = 0; i < gDialogHandlers.SizeOf(); i++) {
  197.         oneHandler = (DialogHandlersPtr)*gDialogHandlers[i];
  198.  
  199.         if (oneHandler->theDialog == theDialog) {
  200.             SetPort(theDialog);
  201.             if (oneHandler->idleHandler != NULL)
  202.                 oneHandler->idleHandler(theDialog);
  203.             break;
  204.         }
  205.     }
  206.     SetPort(savePort);
  207. } // END HandleIdleModeless
  208.  
  209. // ---------------------------------------------------------------------------
  210.  
  211. Boolean IsMovableModal(WindowPtr thisWindow) {
  212.     if (thisWindow == NULL)
  213.         return(false);
  214.     else
  215.         return(GetWVariant(thisWindow) == movableDBoxProc);
  216. } // END IsMovableModal
  217.  
  218. // ---------------------------------------------------------------------------
  219. // ---------------------------------------------------------------------------
  220. // END MovableModalDialogs.cpp